﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using KZ.Exception;
using KZ.Lottery.BLL;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using KZ.Utilities;

namespace KZ.Lottery
{
    public partial class FrmSetting : Form
    {
        #region Private Variables

        private void FrmSetting_Load(object sender, EventArgs e)
        {
            LoadSetting();
            LoadSettingValue();
        }

        private void LoadSetting()
        {
            try
            {
                string settingID = "'" + KZ_Resource.Setting_CapitalRate + "','" + KZ_Resource.Setting_WinRate + "'";
                List<Setting> settingList = SettingBLL.GetSettingList(settingID);
                txtCapitalRate.Text = settingList.Single(s => s.SettingID == KZ_Resource.Setting_CapitalRate).SettingValue;
                txtWinRate.Text = settingList.Single(s => s.SettingID == KZ_Resource.Setting_WinRate).SettingValue;
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadSetting", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void LoadSettingValue()
        {
            try
            {
                SettingValueList = SettingValueBLL.GetSettingValueList();
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadSettingValue", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void dtgSettingValue_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            dtgSettingValue[colDay.Index, e.RowIndex].Value = e.RowIndex + 1;
        }

        private void dtgSettingValue_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == colValue.Index)
                {
                    decimal value = Utility.DecimalParse(dtgSettingValue[colValue.Index, e.RowIndex].Value);
                    if (e.RowIndex > 0)
                    {
                        decimal previous = Utility.DecimalParse(dtgSettingValue[colValue.Index, e.RowIndex - 1].Value);
                        if (value < previous) { throw new BusinessLogicException(KZ_Message.BLM_ERR_DAY_GREATER_PREVIOUSDAY); }
                        //if (previous == 0) { throw new BusinessLogicException(KZ_Message.BLM_ERR_PREVIOUSDAY_REQUIRE); }
                    }
                    dtgSettingValue[colValue.Index, e.RowIndex].Value = Utility.DecimalParse(value);
                }
            }
            catch (BusinessLogicException ex)
            {
                dtgSettingValue[e.ColumnIndex, e.RowIndex].Value = null;
                Global.SetMessage(lblMessage, ex.Message, false);
            }
        }

        private void dtgSettingValue_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                int rowIndex = dtgSettingValue.CurrentCell.RowIndex;
                decimal value = Utility.DecimalParse(dtgSettingValue[colValue.Index, rowIndex].Value);
                if (rowIndex == dtgSettingValue.RowCount - 1 && value > 0)
                {
                    dtgSettingValue.Rows.Add();
                    dtgSettingValue.CurrentCell = dtgSettingValue[colValue.Index, dtgSettingValue.RowCount - 1];
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                List<Setting> settingList = new List<Setting>();
                settingList.Add(new Setting { SettingID = KZ_Resource.Setting_CapitalRate, SettingValue = txtCapitalRate.Text });
                settingList.Add(new Setting { SettingID = KZ_Resource.Setting_WinRate, SettingValue = txtWinRate.Text });

                if (SettingBLL.UpdateSetting(settingList) &&
                    SettingValueBLL.UpdateSettingValue(SettingValueList))
                {
                    Global.SetMessage(lblMessage, KZ_Message.BLM_SUC_SETTING_UPDATE, true);
                    Global.WinRate = Utility.DecimalParse(txtWinRate.Text);
                    Global.CapitalRate = Utility.DecimalParse(txtCapitalRate.Text);
                }
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("btnSave_Click", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        #endregion Private Variables

        #region Constructors

        public FrmSetting()
        {
            InitializeComponent();
            dtgSettingValue.AutoGenerateColumns = false;

            colValue.DefaultCellStyle.Format = "N" + Global.Culture.NumberFormat.CurrencyDecimalDigits;
        }

        #endregion Constructors

        #region Public Members

        public List<SettingValue> SettingValueList
        {
            get
            {
                List<SettingValue> settingValueList = new List<SettingValue>();
                foreach (DataGridViewRow row in dtgSettingValue.Rows)
                {
                    decimal value = Utility.DecimalParse(row.Cells[colValue.Index].Value);
                    if ((value > 0 && row.Index == dtgSettingValue.RowCount - 1) ||
                        row.Index < dtgSettingValue.RowCount - 1)
                    {
                        settingValueList.Add(new SettingValue
                        {
                            Day = Utility.IntParse(row.Cells[colDay.Index].Value),
                            Value = value
                        });
                    }
                }
                return settingValueList;
            }
            set
            {
                dtgSettingValue.Rows.Clear();
                dtgSettingValue.Rows.Add(value.Count + 1);

                for (int i = 0; i < value.Count; i++)
                {
                    dtgSettingValue[colDay.Index, i].Value = value[i].Day;
                    dtgSettingValue[colValue.Index, i].Value = value[i].Value;
                }

                dtgSettingValue[colDay.Index, value.Count].Value = value.Count + 1;
            }
        }

        #endregion Public Members

        private void txtOriginalAmount_KeyPress(object sender, KeyPressEventArgs e)
        {
        }

        private void txtOriginalAmount_KeyUp(object sender, KeyEventArgs e)
        {
        }

        private void dtgSettingValue_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                foreach (DataGridViewRow row in dtgSettingValue.SelectedRows)
                {
                    dtgSettingValue.Rows.Remove(row);
                }

                foreach (DataGridViewRow row in dtgSettingValue.Rows)
                {
                    row.Cells[colDay.Index].Value = row.Index + 1;
                }
            }
        }
    }
}